
package yznu_Bean_handle;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import Order_Utils.Order;
import yznu_jdbc.JDpoor;

public class mainStoreOrder {

	int size=13;
	Connection con = new JDpoor().CreateCon();
	Statement sql = null;
	PreparedStatement pre = null;
	ResultSet rs = null;
	boolean f = false;
	
	public ArrayList<Order> selectAll(int currentPage) {
		// TODO Auto-generated method stub
		ArrayList<Order> orders = new ArrayList<Order>();
		//姓名     手机号     总价       操作 
		try {
			sql = con.createStatement();
			rs=sql.executeQuery("select sum(total)total,user from order_info group by user limit "+(currentPage-1)*size+","+size);
			while(rs.next()){
	    		Order order = new Order();
	    		order.setUser(rs.getString("user"));
	    		order.setTotal(rs.getString("total"));
				orders.add(order);
	    	}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return orders;
	}

	public int countPage(){
		int countPage=0;
		try {
			pre = con.prepareStatement("select count(distinct user) total from order_info");
			rs=pre.executeQuery();
			if(rs.next()){
				int total=rs.getInt("total");
				countPage=total%size==0?total/size:total/size+1;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return countPage;
	}

	public ArrayList<Order> search(String text) {
		// TODO Auto-generated method stub
		ArrayList<Order> orders = new ArrayList<Order>();
		String condition = "select sum(total)total,user from order_info where user like('%"+text+"%') group by user";
		
		try {
			sql = con.createStatement();
			rs=sql.executeQuery(condition);
			while(rs.next()){
	    		Order order = new Order();
	    		order.setUser(rs.getString("user"));
	    		order.setTotal(rs.getString("total"));
				orders.add(order);
	    	}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return orders;
	}
}
